import datetime
from rest_framework import serializers
from .models import Strategy, SensitiveWord, SecretFile, StrategyGlobalAction, CreditGlobalAction, \
    SafetyCreditEvaluation


class DurationInSecondsField(serializers.IntegerField):
    def __init__(self, **kwargs):
        max_value = kwargs.get('max_value')
        min_value = kwargs.get('min_value', 0)

        if max_value is not None:
            kwargs['max_value'] = datetime.timedelta(seconds=max_value)
        kwargs['min_value'] = datetime.timedelta(seconds=min_value)
        super().__init__(**kwargs)

    def to_internal_value(self, value):
        if isinstance(value, datetime.timedelta):
            return value
        return datetime.timedelta(seconds=value)

    def to_representation(self, value):
        return value.days * 86400 + value.seconds


class StrategySerializer(serializers.ModelSerializer):
    class Meta:
        model = Strategy
        fields = '__all__'
        read_only_fields = ('name', 'description', 'level', 'weight', 'category', 'uid')


class StrategyGlobalConfigSerializer(serializers.ModelSerializer):
    action = serializers.SerializerMethodField()

    class Meta:
        model = StrategyGlobalAction
        fields = ['id', 'level', 'action', 'status', 'enabled', 'action_period']

    def get_action(self, instance):
        return instance.get_action_display()


class StrategyCreditGlobalSerializer(serializers.ModelSerializer):

    class Meta:
        model = CreditGlobalAction
        fields = "__all__"


class StrategyCreditBasicSerializer(serializers.ModelSerializer):

    class Meta:
        model = SafetyCreditEvaluation
        fields = "__all__"


class StrategyCreditConfigSerializer(serializers.ModelSerializer):

    class Meta:
        model = CreditGlobalAction
        fields = ["enabled"]

class StrategyGlobalActionSerializer(serializers.ModelSerializer):
    # action = serializers.SerializerMethodField()

    class Meta:
        model = StrategyGlobalAction
        fields = "__all__"


class DeviceLoginLocationSerializer(serializers.Serializer):
    device_uuid = serializers.CharField(write_only=True, max_length=100)
    client_ip = serializers.CharField(write_only=True)

    def create(self, validated_data):
        raise NotImplementedError('This serializer is for data validation only.')

    def update(self, instance, validated_data):
        raise NotImplementedError('This serializer is for data validation only.')



class NewLoginLocationSerializer(serializers.Serializer):
    username = serializers.CharField(write_only=True, max_length=100)
    client_ip = serializers.CharField(write_only=True)

    def create(self, validated_data):
        raise NotImplementedError('This serializer is for data validation only.')

    def update(self, instance, validated_data):
        raise NotImplementedError('This serializer is for data validation only.')


class NewLoginDeviceSerializer(serializers.Serializer):
    username = serializers.CharField(write_only=True, max_length=100)
    # imei = serializers.CharField(write_only=True)
    uuid = serializers.CharField(write_only=True)

    def create(self, validated_data):
        raise NotImplementedError('This serializer is for data validation only.')

    def update(self, instance, validated_data):
        raise NotImplementedError('This serializer is for data validation only.')


class AnomalyDetectionSerializer(serializers.Serializer):
    username = serializers.CharField(write_only=True, max_length=100)

    def create(self, validated_data):
        raise NotImplementedError('This serializer is for data validation only.')

    def update(self, instance, validated_data):
        raise NotImplementedError('This serializer is for data validation only.')


class StrategyTriggerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Strategy
        exclude = ('description',)
        read_only_fields = ('name', 'level', 'weight', 'category', 'uid', 'enabled')


class SensitiveWordSerializer(serializers.ModelSerializer):
    class Meta:
        model = SensitiveWord
        fields = '__all__'


class SecretFileSerializer(serializers.ModelSerializer):
    class Meta:
        model = SecretFile
        fields = '__all__'
